package v1

import (
	"auth/authentication"
	"auth/bll"
	"auth/errors"
	"auth/model"
	"auth/server/web/middleware"
	"auth/utils"
	"github.com/gin-gonic/gin"
	"strconv"
	"strings"
)

var User = &user{}

type user struct{}

func (a *user) Init(r *gin.RouterGroup) {

	r.POST("/login", a.login)
	g := r.Group("/user", middleware.Auth())
	//g := r.Group("/user")
	{
		g.POST("/create", a.createSysUser)
		g.POST("/update", a.updateSysUser)
		g.POST("/delete", a.deleteSysUser)
		g.POST("/list", a.list)
		g.POST("/info", a.findByID)
		g.POST("/isDisabled", a.isDisabled)         // 是否禁用
		g.POST("/changePassword", a.changePassword) // 修改密码
		g.POST("/resetPassword", a.resetPassword)   // 重置密码
	}
	//sys
	{
		g.POST("/setUserAuthorities", a.setUserAuthorities)
		g.POST("/getSysUserList", a.getSysUserList) // 分页获取系统用户列表
		g.POST("/setSysUser", a.createSysUser)      // set系统用户
		g.POST("/sysInfo", a.findSysById)           // 系统用户
	}

	r.POST("/app/login", a.appLogin)
	r.POST("/app/forgot_password", a.forgotPassword)
	r.POST("/app/phone_bind/wx", a.wxAccountBind)
	r.POST("/app/phone_bind/apple", a.appleAccountBind)

	appg := r.Group("/app/user", middleware.Auth())
	{
		appg.GET("/info", a.appUserInfo)                //用户详情
		appg.POST("/replace_phone", a.replacePhone)     // 用户换手机号
		appg.POST("/set_password", a.setPassword)       // 用户设置密码
		appg.POST("/update_password", a.updatePassword) // 用户更换密码
		appg.POST("/update", a.appUpdate)
		appg.POST("/delete", a.deleteAppUser) // APP用户注销
	}
}

func (a *user) login(c *gin.Context) {
	var (
		u   = &model.UserLoginRequest{}
		out *model.UserLoginResponse
		err error
	)

	if err = c.ShouldBindJSON(u); err != nil {
		c.Error(err)
		return
	}
	if out, err = bll.User.Login(c.Request.Context(), u); err != nil {
		c.Error(err)
		return
	}
	c.Header("X-UserID", strconv.FormatInt(1, 10))

	utils.ResponseOK(c, out)

}

func (a *user) refresh(c *gin.Context) {
	authorization := c.GetHeader("Authorization")
	parts := strings.SplitN(authorization, " ", 2)
	if !(len(parts) == 2 && parts[0] == "Bearer") {
		c.Error(errors.New("刷新失败"))
		return
	}

	token, err := authentication.Refresh(parts[1])
	if err != nil {
		c.Error(errors.New("刷新失败"))
		return
	}
	utils.ResponseOK(c, token)
}

// isDisabled 是否禁用
func (a *user) isDisabled(c *gin.Context) {
	var (
		in  = &model.IsDisabledRequest{}
		err error
	)
	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}
	if err = bll.User.IsDisabled(in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, nil)
}

func (a *user) changePassword(c *gin.Context) {
	var (
		in  = &model.ChangePasswordRequest{}
		err error
	)
	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}
	if err = bll.User.ChangePassword(in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, nil)
}

func (a *user) resetPassword(c *gin.Context) {
	var (
		in  = &model.ResetPasswordRequest{}
		err error
	)
	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}
	if err = bll.User.ResetPassword(in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, nil)
}

func (a *user) createSysUser(c *gin.Context) {
	var (
		in  = &model.UserCreateRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.User.Create(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, nil)
}

func (a *user) findSysById(c *gin.Context) {
	var (
		in  = &model.FindSysByIdRequest{}
		out = &model.FindSysByIdResponse{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}
	if out, err = bll.User.FindSysById(in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, out)
}

func (a *user) updateSysUser(c *gin.Context) {
	var (
		in  = &model.UserUpdateRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.User.Update(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, nil)
}

func (a *user) list(c *gin.Context) {
	var (
		in  = &model.UserListRequest{}
		out = &model.UserListResponse{}
		err error
	)

	if err = c.ShouldBind(in); err != nil {
		c.Error(err)
		return
	}

	if out, err = bll.User.List(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, out)
}

func (a *user) getSysUserList(c *gin.Context) {
	var (
		in  = &model.GetSysUserListRequest{}
		out = &model.GetSysUserListResponse{}
		err error
	)
	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}
	if out, err = bll.User.GetSysUserList(in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, out)
}

// 删除系统用户
func (a *user) deleteSysUser(c *gin.Context) {
	var (
		in  = &model.UserDeleteRequest{}
		err error
	)
	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}
	jwtId, err := authentication.ContextUserID(c.Request.Context())
	if in.ID == jwtId {
		c.Error(errors.CannotDeleteItself.Error())
		return
	}
	if err = bll.User.DeleteSysUser(in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, nil)
}

// App用户注销
func (a *user) deleteAppUser(c *gin.Context) {
	var (
		err error
	)

	jwtId, err := authentication.ContextUserID(c.Request.Context())
	if err != nil {
		c.Error(err)
		return
	}

	in := &model.UserDeleteRequest{
		ID: jwtId,
	}
	if err = bll.User.DeleteSysUser(in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, nil)
}

func (a *user) findByID(c *gin.Context) {
	var (
		in  = &model.UserBaseRequest{}
		out = &model.UserInfoResponse{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if out, err = bll.User.FindById(in.ID); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, out)

}

// 设置用户角色权限 setUserAuthorities
func (a *user) setUserAuthorities(c *gin.Context) {
	var (
		in  = &model.SetUserAuthoritiesRequest{}
		err error
	)
	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}
	if err = bll.User.SetUserAuthorities(in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, nil)
}
